﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LocalizeHelper.cs" company="None">
//   <Author>Adrian von Allmen, 2013</Author>
// </copyright>
// <summary>
//   Helper Class for Localization
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CrumbleLibrary
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Resources;
    using System.Xml.Linq;

    /// <summary>
    /// Helper Class for Localization
    /// </summary>
    public class LocalizationHelper
    {
        /// <summary>
        /// The reference language.
        /// </summary>
        private readonly CultureInfo referenceLanguage = CultureInfo.CurrentCulture;

        /// <summary>
        /// Initializes a new instance of the <see cref="LocalizationHelper"/> class.
        /// </summary>
        /// <param name="value"> The value. </param>
        public LocalizationHelper(CultureInfo value)
        {
            this.referenceLanguage = value;
        }

        /// <summary>
        /// The localized string.
        /// </summary>
        /// <param name="keyName"> The key nameThe ID within  Source. </param>
        /// <param name="languageCultureInfo"> Language culture info. </param>
        /// <param name="nullValue"> Fallback-String if nothing found </param>
        /// <returns> Localized String </returns>
        public static string LocalizedString(string keyName, CultureInfo languageCultureInfo, string nullValue = null)
        {
            string returnValue;

            try
            {
                returnValue = HelperLibrary.GetDefaultTranslationFile().GetString(keyName, languageCultureInfo);
                returnValue = returnValue != string.Empty ? returnValue : (nullValue ?? string.Empty);
            }
            catch
            {
                returnValue = nullValue ?? string.Empty;
            }

            return returnValue;
        }

        /// <summary>
        /// Localize a string
        /// </summary>
        /// <param name="resourceFile"> A single ResourceFile as translation resource</param>
        /// <param name="keyName"> The ResourceID. </param>
        /// <param name="languageCultureInfo"> The language culture info. </param>
        /// <param name="nullValue"> Fallback String if no translation could be found </param>
        /// <returns> The translated string</returns>
        public static string LocalizedString(ResourceManager resourceFile, string keyName, CultureInfo languageCultureInfo, string nullValue = null)
        {
            string returnValue;

            try
            {
                returnValue = resourceFile.GetString(keyName, languageCultureInfo);
                returnValue = returnValue != string.Empty ? returnValue : (nullValue ?? string.Empty);
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch
            {
                returnValue = nullValue ?? string.Empty;
            }

            return returnValue;
        }

        /// <summary>
        /// Localize a string
        /// </summary>
        /// <param name="resourceFiles"> A collection of ResourceFiles as translation resources</param>
        /// <param name="keyName"> The ResourceID. </param>
        /// <param name="languageCultureInfo"> The language culture info. </param>
        /// <param name="nullValue"> Fallback String if no translation could be found </param>
        /// <returns> The translated string</returns>
        public static string LocalizedString(IEnumerable<ResourceManager> resourceFiles, string keyName, CultureInfo languageCultureInfo, string nullValue = null)
        {
            foreach (var listItem in resourceFiles)
            {
                string returnValue;
                try
                {
                    returnValue = listItem.GetString(keyName, languageCultureInfo);
                    returnValue = returnValue ?? (nullValue ?? string.Empty);
                }
                // ReSharper disable once EmptyGeneralCatchClause
                catch
                {
                    returnValue = string.Empty;
                }

                if (!returnValue.Equals(string.Empty))
                {
                    return returnValue;
                }
            }

            return nullValue ?? string.Empty;
        }

        /// <summary>
        /// The translate report.
        /// </summary>
        /// <param name="reportType"> The report Type. </param>
        /// <param name="reportObject"> The report object.   </param>
        /// <param name="languageValue"> The language Value.  </param>
        /// <param name="translationFile"> A single resource manager File </param>
        /// <returns> Stream refactored with translation   </returns>
        public static Stream TranslateReport(HelperLibrary.ReportType reportType, object reportObject, CultureInfo languageValue, ResourceManager translationFile)
        {
            var temporaryList = new List<ResourceManager>();
            temporaryList.Add(translationFile);
            var streamValue = TranslateReport(reportType, reportObject, languageValue, temporaryList);
            return streamValue;
        }

        /// <summary>
        /// The translate report.
        /// </summary>
        /// <param name="reportType"> The report Type. </param>
        /// <param name="reportObject"> The report Object. </param>
        /// <param name="languageValue"> The language Value.  </param>
        /// <param name="translationFiles"> A collection of translation files </param>
        /// <returns> Stream refactored with translation   </returns>
        public static Stream TranslateReport(HelperLibrary.ReportType reportType, object reportObject, CultureInfo languageValue, List<ResourceManager> translationFiles)
        {
            if (reportType == HelperLibrary.ReportType.MSRS)
            {
                var reportStream = reportObject as Stream;

                if (reportStream != null)
                {
                    var reportXml = XDocument.Load(reportStream);

                    foreach (var element in
                        reportXml.Descendants(
                            XName.Get(
                                "Value", @"http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition")))
                    {
                        var attribute =
                            element.Attribute(
                                XName.Get("LocID", @"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"));

                        if (attribute == null)
                        {
                            continue;
                        }

                        var translatedValue = LocalizedString(
                            translationFiles, attribute.Value, languageValue, element.Value);

                        // If Report-Element contain ExpressionFields: Use Replace on LocID as Reference
                        if (element.Value.Contains("=") && !string.IsNullOrEmpty(translatedValue))
                        {
                            element.Value = element.Value.Replace(attribute.Value, translatedValue);
                        }
                        else
                        {
                            element.Value = string.IsNullOrEmpty(translatedValue) ? element.Value : translatedValue;
                        }
                    }

                    Stream ms = new MemoryStream();
                    reportXml.Save(ms, SaveOptions.OmitDuplicateNamespaces);
                    ms.Position = 0;

                    return ms;
                }
            }

            return null;
        }

        /// <summary>
        /// The localized string.
        /// </summary>
        /// <param name="keyName"> The key nameThe ID within  Source. </param>
        /// <param name="nullValue"> Fallback-String if nothing found </param>
        /// <returns> Localized String </returns>
        public string LocalizedString(string keyName, string nullValue = null)
        {
            string returnValue;

            try
            {
                returnValue = HelperLibrary.GetDefaultTranslationFile().GetString(keyName, this.referenceLanguage);
                returnValue = returnValue != string.Empty ? returnValue : (nullValue ?? string.Empty);
            }
            catch
            {
                returnValue = nullValue ?? string.Empty;
            }

            return returnValue;
        }
    }
}
